
module WindowDefineNewHaircut (windowDefineNewHaircut) where

import Graphics.UI.Gtk
import Data.IORef
import MainHandler
import Haircut
import WindowUtils

windowDefineNewHaircut haircutsDb = do
    initGUI
    window <- windowNew
    windowSetPosition window WinPosCenter
    windowSetTitle window "Definir nuevo corte"
    
    (label1,frameCortesGenerales) <- myLabelWithFrameNew
    labelSetText label1 "Cortes Generales"
    (label2,frameNuevoCorte) <- myLabelWithFrameNew
    labelSetText label2 "Nuevo Corte"
    labelHcName <- labelNew (Just "nombre:")
    labelHcDesc <- labelNew (Just "descripcion:")
    
    buttonAdd <- buttonNewWithLabel "Agregar nuevo corte"
    buttonDel <- buttonNewWithLabel "Eliminar corte"
    volverButton <- buttonNewWithLabel "Volver"
    volverButton `onClicked` (widgetDestroy window)

    entryName <- entryNew
    entryDescription <- entryNew
    
    buttonAdd `onClicked` addHaircut haircutsDb entryName entryDescription window

    combo <- comboHaircuts haircutsDb
    buttonDel `onClicked` deleteHaircut haircutsDb combo window    
    
    sep <- hSeparatorNew
    
    vbox <- vBoxNew False 5
    
    boxPackStart vbox frameCortesGenerales PackNatural 1
    boxPackStart vbox combo PackNatural 1
    boxPackStart vbox buttonDel PackNatural 1
    boxPackStart vbox sep PackNatural 1
    boxPackStart vbox frameNuevoCorte PackNatural 1
    boxPackStart vbox labelHcName PackNatural 1
    boxPackStart vbox entryName PackNatural 1
    boxPackStart vbox labelHcDesc PackNatural 1
    boxPackStart vbox entryDescription PackNatural 1
    boxPackStart vbox labelHcDesc PackNatural 1
    boxPackStart vbox buttonAdd PackNatural 1
    boxPackStart vbox volverButton PackNatural 1
    
    set window [windowDefaultWidth := 300, windowDefaultHeight := 300,
              containerBorderWidth := 10, containerChild := vbox]
              
    widgetShowAll window
    
    

--------------------------------------------------------------------------
----------------------FUNCIONES DE CONTROL--------------------------------
--------------------------------------------------------------------------

comboHaircuts db = do
 
    haircuts <- readIORef db
    store <- listStoreNew (getBaseHaircuts haircuts)
    combo <- comboBoxEntryNewWithModel store
    customStoreSetColumn store textColumn (getHaircutName)
    comboBoxEntrySetTextColumn combo textColumn
  
    return combo

textColumn :: ColumnId Haircut String
textColumn = makeColumnIdString 0

addHaircut db entryName entryDesc win = do
    name <- entryGetText entryName
    description <- entryGetText entryDesc
    
    let newHc = createHaircut name description
    modifyIORef db (refAddHaircut newHc)
    addMsg <- messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Corte agregado"
    dialogRun addMsg
    
    widgetDestroy win

deleteHaircut db combo win = do
    index <- comboBoxGetActive combo
    if (index < 0) 
     then return ()
     else do
            (modifyIORef db (deleteHcFromDb index))
            addMsg <- (messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Corte borrado")
            dialogRun addMsg
            widgetDestroy win 
            return ()
 
 
-- TODO
deleteHcFromDb index handler = let newHcList = delHaircut index (getBaseHaircuts handler)
                                in setHaircutsList handler newHcList
    
delHaircut 0 (hc:hcs) = hcs
delHaircut n (hc:hcs) = hc : (delHaircut (n-1) hcs)
 
refAddHaircut :: Haircut -> MainHandler -> MainHandler
refAddHaircut hc handler = addBaseHaircutToHandler handler hc
   
    
    
    